package or.exa.cangguan.service.impl;

import cn.hutool.core.bean.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import or.exa.cangguan.common.Page;
import or.exa.cangguan.common.Redis;
import or.exa.cangguan.entity.dto.AreaDto;
import or.exa.cangguan.entity.po.Area;
import or.exa.cangguan.entity.vo.AreaVo;
import or.exa.cangguan.mapper.AreaMapper;
import or.exa.cangguan.mapper.StoreMapper;
import or.exa.cangguan.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class AreaServiceImpl implements AreaService {

    @Autowired
    private AreaMapper areaMapper;


    @Autowired
    private StoreMapper  storeMapper;

    @Autowired
    private Redis redis;



    //新增库区
    @Override
    public int addArea(AreaVo areaVo) {
        Date date = new Date(System.currentTimeMillis());

        Area area = new Area();
        BeanUtil.copyProperties(areaVo, area);
        area.setStoreId(storeMapper.selectByName(areaVo.getStoreName()));
        area.setState("0");
        area.setCreateTime(date);
        area.setUpdateTime(date);
        area.setCreateBy(0);
        area.setUpdateBy(0);
        area.setAdminId(0);

        int insert = areaMapper.insert(area);
        redis.del("storeName");



        return insert;
    }


    //更新库区
    @Override
    public int updateArea(AreaVo areaVo) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        Area area = new Area();
        BeanUtil.copyProperties(areaVo,area);
        Integer storeId = storeMapper.selectByName(areaVo.getStoreName());
        area.setStoreId(storeId);
        area.setUpdateTime(date);
        log.info("----------------------"+area);

        int i = areaMapper.updateByPrimaryKeySelective(area);
        redis.del("storeName");


        return i;
    }

    //查询库区
    @Override
    public Page searchArea(AreaVo areaVo) {

        log.info("----------------------"+areaVo);

        Page page = new Page();
        String storeName= areaVo.getStoreName();
        Integer storeId=null;
        if (storeName != null && storeName!= "") {
            storeId = storeMapper.selectByName(storeName);
        }
        BeanUtil.copyProperties(areaVo, page);
        List<AreaDto > areaDtos= areaMapper.selectArea(areaVo, storeId, page);
        page.setResultList(areaDtos);
        int totalNum = areaMapper.selectAreaCount(areaVo, storeId);
        page.setTotalNum(totalNum);

        return page;
    }


    //删除库区
    @Override
    public int deleteArea(Integer id) {
        int i = areaMapper.deleteByPrimaryKey(id);
        redis.del("storeName");


        return i;
    }


    //查询所有库区名称
    @Override
    public List<String> selectName() {
        if (redis.idExists("areaName")){
            return (List<String>) redis.get("areaName");
        }
        else {
            List<String> areaName = areaMapper.selectName();
            redis.set("areaName", areaName);
            return areaName;
        }
    }
}
